@extends('layouts.adminlayout')

@section('main')

<p>{{ link_to_route('categories.create', 'Thêm chuyên mục bài viết',null,array('class'=>'btn btn-info')) }}</p>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <h3 class="box-title">Chuyên mục <small>Danh sách chuyên mục</small></h3>
                </div><!-- /.box-header -->
                <div class="box-body table-responsive">
                    @if ($chuyen_muc_bai_viets->count())
                    <table id="dataTable" class="table table-hover table-striped">
                        <thead>
                            <tr>
                                <th>Tên Chuyên Mục</th>
                                <th>Sửa</th>
                                <th>Xóa</th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <th>Tên Chuyên Mục</th>
                                <td></td>
                                <td></td>
                            </tr>
                        </tfoot>
                        <tbody>
                            @foreach ($chuyen_muc_bai_viets as $chuyen_muc_bai_viet)
                            <tr>
                                <td>{{{ $chuyen_muc_bai_viet->ten_chuyen_muc }}}</td>
                                <td>{{ link_to_route('categories.edit', '', array($chuyen_muc_bai_viet->id), array('class' => 'btn btn-info fa fa-edit')) }}</td>
                                <td>
                                    {{ Form::open(array('method' => 'DELETE', 'route' => array('categories.destroy', $chuyen_muc_bai_viet->id),'class'=>'delete_chuyen_muc')) }}
                                    <button type="submit" class="btn btn-danger"><li class="fa fa-trash-o"></li></button>
                                    {{ Form::close() }}
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                    @else
                    <p>Không có chuyên mục bài viết</p>
                    @endif
                </div><!-- /.box-body -->
            </div><!-- /.box -->
        </div>
    </div>
</section>
<script src="<?php echo asset('public/admin/js/plugins/datatables/jquery.dataTables.columnFilter.js'); ?>" type="text/javascript"></script>
<script type="text/javascript">
    // Setup - add a text input to each footer cell
        $('#dataTable tfoot th').each(function() {
            var title = $('#dataTable thead th').eq($(this).index()).text();
            $(this).html('<input type="text" placeholder="' + title + '" />');
        });

        var table = $('#dataTable').DataTable({
                    "scrollX": "100%",
                    "scrollCollapse": true,
                    "paging": true,
                    "autoWidth": false
                });
        // Apply the filter
        table.columns().eq(0).each(function(colIdx) {
            $('input', table.column(colIdx).footer()).on('keyup change', function() {
                table
                        .column(colIdx)
                        .search(this.value)
                        .draw();
            });
        });
</script>
<script type="text/javascript">
    $('.delete_chuyen_muc').submit(function(event) {
       var txt;
        var r = confirm("Xác nhận xóa?");
        if (r == true) {
            $.ajax({
                url: $(this).attr('action'),
                type: 'delete',
                dataType: 'json',
                data: {},
            })
            .done(function() {
                console.log("success");
                window.location.reload();
            })
            .fail(function() {
                alert('Không thể xóa vì có bài viết thuộc chuyên mục này!!!');
            })
            .always(function() {
                console.log("complete");
            });
            return false;
            
        } else {
            return false;
        }
        return false;
    });
</script>
@stop